package com.flexcloudsdk.fetch;

import java.util.logging.Logger;

import java.util.List;
import java.net.URL;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.HTTPHeader;

public class CloudHttpResponse {
	private static final Logger log = Logger.getLogger(CloudHttpResponse.class.getName());
	private  HTTPResponse response;
	private String sourceClass;
	private String sourceMethod;

	public CloudHttpResponse() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudHttpResponse.class.getName();

		log.info("CloudHttpResponse has been created by BlazeDS");
	}
	
	public void setResponse(HTTPResponse value) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		response = value;
	}
	
	public byte[] getContent() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		byte[] data = null;
		if (response != null)
			data = response.getContent();
		return data;
	}
	
	public URL getFinalUrl() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		URL url = null;
		if (response != null)
			url = response.getFinalUrl();
		return url;
	}
	
	public List<HTTPHeader> getHeaders() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		List<HTTPHeader>	headers = null;
		if (response != null)
			headers = response.getHeaders();
		return headers;
	}
	
	public int getResponseCode() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		int responseCode = 0;	
		if (response != null)
			responseCode = response.getResponseCode();
		return responseCode;
	}
	
}
